package com.security.test.config;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.security.test.entity.MyUserDetail;
import com.security.test.entity.User;
import com.security.test.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

/**
 * @author songmin.hou
 */
@Component
@Slf4j
public class MyUserDetailService implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public MyUserDetail loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, username));
        if (ObjectUtil.isEmpty(user)) {
            System.out.println("用户不存在");
            throw new RuntimeException("用户不存在");
        }
        log.info("qqqqqqq");
        //获取用户的角色信息
        //获取用户的权限信息
        MyUserDetail myUserDetail = new MyUserDetail();
        myUserDetail.setUser(user);
        return myUserDetail;
    }
}
